package com.edu.study.base.rest;

import com.edu.base.common.utils.DateUtils;
import com.edu.base.common.utils.IdGen;
import com.edu.study.base.lmsOrder.domain.LmsOrder;
import com.edu.study.base.lmsOrder.service.ILmsOrderService;
import com.edu.study.base.lmsOrderItem.domain.LmsOrderItem;
import com.edu.study.base.lmsOrderItem.service.ILmsOrderItemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@Api(tags = "9、我的订单相关接口")
@RestController
@RequestMapping("/api/lmsOrder")
public class AppLmsOrderController extends BaseController {
 
	@Autowired
	private ILmsOrderService lmsOrderService;

	@Autowired
	private ILmsOrderItemService lmsOrderItemService;
    
	/**
     * 查询会员 
     */ 
    @GetMapping("/list")
    @ApiOperation(value= "会员订单",response=LmsOrderItem.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员Id",required = true),
        })
	public TableDataInfo list(String memberId, String productName) {
    	startPage();
    	List<LmsOrderItem>  list = lmsOrderItemService.selectLmsOrderItemByMemberId(memberId, productName);
		return getDataTable(list);
	}

    /**
     *   新增订单信息
         * @Title: add
         * @Description: TODO(这里用一句话描述这个方法的作用)
         * @param @param memberId
         * @param @param courseId
         * @param @param courseIcon
         * @param @param type
         * @param @return 参数
         * @return AjaxResult 返回类型
         * @throws
     */
    @PostMapping("/add")
	@ApiOperation(value = "新增订单信息",response= LmsOrder.class)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "memberId", value = "会员Id",required = true),
        @ApiImplicitParam(name = "sourceType", value = "订单来源：0->PC订单；1->app订单",required = true),
        @ApiImplicitParam(name = "status", value = "订单状态：0->待付款；1->待发货；2->已发货；3->已完成；4->已关闭；5->无效订单",required = true),
        @ApiImplicitParam(name = "orderType", value = "订单类型：0->正常订单；1->秒杀订单",required = true),
        @ApiImplicitParam(name = "payType", value = "支付方式：0->未支付；1->支付宝；2->微信",required = true),
        @ApiImplicitParam(name = "note", value = "订单备注"),
        @ApiImplicitParam(name = "totalAmount", value = "订单总金额"),
        @ApiImplicitParam(name = "payAmount", value = "应付金额"),
        @ApiImplicitParam(name = "promotionAmount", value = "促销优化金额"),
        @ApiImplicitParam(name = "integrationAmount", value = "积分抵扣金额"),
         @ApiImplicitParam(name = "useIntegration", value = "下单时使用的积分"),
        @ApiImplicitParam(name = "productCategory", value = "商品分类: 0: 会员充值，1：购买课程，2：老师批改作业",required = true),
        @ApiImplicitParam(name = "productId", value = "产品id"),		
        @ApiImplicitParam(name = "productCategoryId", value = "商品分类id"),		
        @ApiImplicitParam(name = "productName", value = "产品名称"),		
        @ApiImplicitParam(name = "productPic", value = "产品图片"),		
        @ApiImplicitParam(name = "productPrice", value = "销售价格"),
        @ApiImplicitParam(name = "giftIntegration", value = "产品赠送积分"),	 
   })
    public AjaxResult add(String memberId,Integer sourceType,Integer status,Integer orderType,
    		Integer payType,String note,String totalAmount,String payAmount,
    		String promotionAmount,String integrationAmount,
    		Long useIntegration,
    		String productCategory,String productId,String  productCategoryId,
    		String productName,String productPic,String productPrice,Long giftIntegration
    		)
    {
    	List<LmsOrder> list = lmsOrderService.selectLmsOrderList(new LmsOrder());
    	LmsOrder vo  = new LmsOrder(); 
    	if(CollectionUtils.isEmpty(list)) {
    		vo.setOrderSn(getNewEquipmentNo("wyd", null));
    	}else {
    		// 创建时间倒叙
    		vo.setOrderSn(getNewEquipmentNo("wyd", list.get(0).getOrderSn())); 
    	}
    	vo.setId(UUIDUtils.getUUID());
    	vo.setSourceType(sourceType);
    	vo.setStatus(status);
    	vo.setOrderType(orderType);
    	vo.setPayType(payType);
    	vo.setNote(note);
    	vo.setTotalAmount(new BigDecimal(totalAmount));
    	vo.setPayAmount(new BigDecimal(promotionAmount));
    	vo.setIntegrationAmount(new BigDecimal(integrationAmount));
    	vo.setUseIntegration(useIntegration);
    	  
    	vo.setMemberId(memberId);
    	vo.setModifyTime(DateUtils.getNow());
    	vo.setCreateTime(DateUtils.getNow());
    	vo.setPaymentTime(DateUtils.getNow());
    	
    	// TODO  新增明细 商品分类: 0: 会员充值，1：购买课程，2：老师批改作业
    	if("0".equals(productCategory)) {
    		LmsOrderItem item = new LmsOrderItem();
    		item.setId(IdGen.randomLong());
    		item.setOrderId(vo.getId());
    		item.setOrderSn(vo.getOrderSn());
    		item.setProductId(productId);
    		item.setProductCategoryId(productCategoryId);
    		item.setProductName(productName);
    		item.setProductPic(productPic);
    		item.setProductPrice(new BigDecimal(productPrice));
    		item.setGiftIntegration(giftIntegration);
    		lmsOrderItemService.insertLmsOrderItem(item);
    	}
        return toAjax(lmsOrderService.insertLmsOrder(vo));
    }
    
    
    /**
	 * 生成规则设备编号:设备类型+五位编号（从1开始，不够前补0）
	 * 
	 * @param equipmentType 设备类型
	 * @param equipmentNo 最新设备编号
	 * @return
	 */
	private  String getNewEquipmentNo(String equipmentType, String equipmentNo){
		String newEquipmentNo = equipmentType + "00000001";
 		if(equipmentNo != null && !equipmentNo.isEmpty()){
			int no = Integer.parseInt(equipmentNo);
			int newEquipment = ++no;
			newEquipmentNo = String.format(equipmentType + "%05d", newEquipment);
		} 
		return newEquipmentNo;
	}
 
    
  

}
